﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Ria;

namespace Groceries.Views
{
    public partial class ShoppingLists : Page
    {
        public ShoppingLists()
        {
            InitializeComponent();
        }


        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            shoppingListData.SubmitChanges();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Determine list row clicked
            Groceries.Web.ShoppingList list = shoppingListGrid.SelectedItem as Groceries.Web.ShoppingList;

            // Open list and show items and meals
            ShoppingListDetails shopListDetails = new ShoppingListDetails(list);
            this.Content = shopListDetails;
        }

        private void AddListButton_Click(object sender, RoutedEventArgs e)
        {
            NewShoppingList newList = new NewShoppingList();
            newList.Closed += new EventHandler(newList_Closed);
            newList.Show();
        }

        void newList_Closed(object sender, EventArgs e)
        {
            NewShoppingList newList = (NewShoppingList)sender;
            if (newList.NewList != null)
            {
                Groceries.Web.Services.GroceriesDomainContext ctx = (Groceries.Web.Services.GroceriesDomainContext)shoppingListData.DomainContext;
                ctx.ShoppingLists.Add(newList.NewList);
                ctx.SubmitChanges();
            }
        }

    }
}
